/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import Model.VentaM;
import MsSQL.ConMsSQL;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 *
 * @author Maryluz
 */
public class VentaC {
 
    public int getCountVenta() {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "select COUNT(idventa) from TBL_VENTA";

                Statement instruccionSQL = cnn.createStatement();
                ResultSet rs = instruccionSQL.executeQuery(sql);
                if (rs != null) {
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                return -1;
            }
        } catch (Exception e) {
            //  System.out.print(e);
            return -1;
        }
        return -1;
    }
    
    public int getIdVenta(String numVenta, int idTIENDA) {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "select idventa from TBL_VENTA where numVenta='"+numVenta+"' and idtienda="+idTIENDA;

                Statement instruccionSQL = cnn.createStatement();
                ResultSet rs = instruccionSQL.executeQuery(sql);
                if (rs != null) {
                    while (rs.next()) {
                        return rs.getInt(1);
                    }
                }
                return -1;
            }
        } catch (Exception e) {
            //  System.out.print(e);
            return -1;
        }
        return -1;
    }
    
    public boolean agregarVenta(VentaM ovM) {

        Connection cnn = ConMsSQL.obtenerConexion();
        try {
            if (cnn != null) {
                String sql = "{call sp_addVenta(?,?,?,?,?,?,?,?)}";
                CallableStatement cs = cnn.prepareCall(sql);
                cs.setString(1, ovM.getNumVenta());
                cs.setString(2, ovM.getFecha());
                cs.setInt(3, ovM.getIdTienda());
                cs.setString(4, ovM.getCliente());
                cs.setInt(5, ovM.getTipoDocumento());
                cs.setInt(6, ovM.getIdVendedor());
                cs.setDouble(7, ovM.getTotal());
                cs.setDouble(8, ovM.getDescuento());
                ResultSet rs = cs.executeQuery();
                if (rs != null) {
                    while (rs.next()) {
                       return true;
                    }
                }
                return false;
            }
        } catch (Exception e) {
            // System.out.print(e);
          return false;
        }
        return false;
    }

}
